<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Members extends CI_Model {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
     
    public function __construct(){
        parent::__construct();
        
    }

	/********************************************************
	 * 
	 *     
	 *     UNTUK REGISTRASI DAN UPDATE USER DAN PARTNER
	 * 
	 * 
	 *********************************************************/
    
    function validasi(){
        $username = $this->input->post('username');
        $password = sha1($this->input->post('password'));
        
        $this->db->where('acc_name', $username);
        $this->db->where('password', $password);
        
        $query = $this->db->get('account');            
            
        if($query->num_rows == 1){
            return true;
        }
        else{
    		$this->load->library('messages');
    		$this->load->helper('msg');
            
            $this->messages->add('Username tidak ada. ', 'error');
        }
        
    }
    
    function get_login(){
        $this->db->where('acc_name', $this->input->post('username'));
        $this->db->where('password', sha1($this->input->post('password')));
        return $this->db->get('account');        
    }
    
    function save_log($action){
        $CI =& get_instance();
        
        $accid = $CI->session->userdata('accid');
        
        $data = array('action' => $action,
                      'acc_id' => $accid);
        
        $insert = $this->db->insert('log',$data);
        return $insert;
    }

	/****************************************************************************
	 * 
	 *     
	 *     UNTUK MENGAMBIL INFORMASI USER
	 * 
	 * 
	 *****************************************************************************/
    
    function get_users(){
        return $this->db->get('account');
    }
    
    function get_user($id){
        $this->db->where('acc_id',$id);
        return $this->db->get('account');
    }
    
    function hapusUser($id){
        $this->db->where('acc_id',$id);
        $query = $this->db->delete('account');
        
        return $query;
    }

	/********************************************************
	 * 
	 *     
	 *     UNTUK MENAMBAH DAN UPDATE USER
	 * 
	 * 
	 *********************************************************/
    
    function tambahUser(){
        $CI =& get_instance();
        
        $creator = $CI->session->userdata('username');
        $username = $this->input->post('namaAkun');
        $password = $this->input->post('password');
        
        $now = date("Y-m-d H:i:s");
        
        $data = array(
                'acc_name' => $username,
                'user_name' => $this->input->post('namaPengguna'),
                'password' => sha1($password),
                'contact' => $this->input->post('nokontak'),
                'type' => $this->input->post('tipe')
                );
        
        $insert = $this->db->insert('account', $data);
        
        $accid = $this->session->userdata('accid');
        
        $log = array(
                'acc_id' => $accid,
                'action' => "$creator membuat akun baru dengan nama akun $username");
                
        $insert = $this->db->insert('log', $log);
        
        return true;
    }
    
    function ubahUser($id){
        $CI =& get_instance();
        
        $creator = $CI->session->userdata('username');
        $username = $this->input->post('namaAkun');
        $password = $this->input->post('password');
        
        $now = date("Y-m-d H:i:s");
        
        $data = array(
                'acc_name' => $username,
                'user_name' => $this->input->post('namaPengguna'),
                'password' => sha1($password),
                'contact' => $this->input->post('nokontak'),
                'type' => $this->input->post('tipe')
                );
        
        $this->db->where('acc_id',$id);
        $this->db->update('account', $data);
        
        $accid = $this->session->userdata('accid');
        
        $log = array(
                'acc_id' => $accid,
                'action' => "$creator mengubah informasi akun $username");
                
        $insert = $this->db->insert('log', $log);
        
        return true;
    }

	/********************************************************
	 * 
	 *     
	 *     UNTUK MENGAMBIL INFORMASI LOGS
	 * 
	 * 
	 *********************************************************/
     
     function get_all_logs($limit,$offset){
        $query = $this->db->query("SELECT log.time, account.acc_name, log.action FROM log JOIN account ON log.acc_id = account.acc_id ORDER BY time DESC LIMIT $offset, $limit");
        return $query;
     }
     
     function total_logs(){
        $query = $this->db->get('log');
        return $query->num_rows();
     }

	/********************************************************
	 * 
	 *     
	 *     UNTUK FILTER LOGS
	 * 
	 * 
	 *********************************************************/
     
     function semua_log(){
        $query = $this->db->query("SELECT log.time, account.acc_name, log.action FROM log JOIN account ON log.acc_id = account.acc_id ORDER BY time DESC");
        return $query;
     }
     
     function cariUser($cariAkun,$dari,$sampai){
        $query = $this->db->query("SELECT log.time, account.acc_name, log.action FROM log JOIN account ON log.acc_id = account.acc_id WHERE (account.acc_name ='$cariAkun' AND log.time BETWEEN '$dari' AND '$sampai') OR (account.acc_name ='$cariAkun') OR (log.time BETWEEN '$dari' AND '$sampai') ORDER BY time DESC");
        return $query;
     }
     
     
     function hasilCari($cariAkun,$dari,$sampai,$limit,$offset){
        $query = $this->db->query("SELECT log.time, account.acc_name, log.action FROM log JOIN account ON log.acc_id = account.acc_id WHERE (account.acc_name ='$cariAkun' AND log.time BETWEEN '$dari' AND '$sampai') OR (account.acc_name ='$cariAkun') OR (log.time BETWEEN '$dari' AND '$sampai') ORDER BY time DESC");
        return $query;
     }
     
     function total_hasilcari_logs($cariAkun,$dari,$sampai){
        $query = $this->db->query("SELECT log.time, account.acc_name, log.action FROM log JOIN account ON log.acc_id = account.acc_id WHERE (account.acc_name ='$cariAkun' AND log.time BETWEEN '$dari' AND '$sampai') OR (account.acc_name ='$cariAkun') OR (log.time BETWEEN '$dari' AND '$sampai') ORDER BY time DESC");
        return $query->num_rows();
     }
     
     
}

/* End of file membership.php */
/* Location: ./application/model/membership.php */